﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mamnontuoitho7.AppCode;
using System.Data;

namespace mamnontuoitho7.backend
{
    public partial class ClipEdit : System.Web.UI.Page
    {
        private Product _product = new Product();
        private VideoClip _videoClip = new VideoClip();
        protected DataTable _dt;
        protected string _productID = "";

        protected void Page_Load(object sender, EventArgs e)
        {

            //Check Role Admin
            if (Session["memberID"].ToString() != "" && (ConstantClass.permissionArticle).IndexOf(Session["role"].ToString() + ",") != -1)
            {
                try
                {
                    if (Request.QueryString["productID"] != null)
                    {
                        _productID = Request.QueryString["productID"].ToString();
                    }

                    _dt = _videoClip.GetVideoByID(_productID);

                    if (!IsPostBack)
                    {
                        txtTitleClip.Text = _dt.Rows[0]["Title"].ToString();
                        txtDescription.Text = _dt.Rows[0]["Introduce"].ToString();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Response.Redirect(ConstantClass._errorAdminPage);
                }
            }
            else
            {
                Response.Redirect(ConstantClass._errorLoginPage);
            }
        }

        protected void btnClipEdit_Click(object sender, EventArgs e)
        {
            try
            {
                string videoURL = txtURLClip.Text.Trim();
                if (videoURL == "")
                {
                    _videoClip.VideoUpdate(_productID, txtTitleClip.Text.Trim(), txtDescription.Text.Trim(), Session["username"].ToString(), _dt.Rows[0]["VideoURL"].ToString());
                }
                else
                {
                    _videoClip.VideoUpdate(_productID, txtTitleClip.Text.Trim(), txtDescription.Text.Trim(), Session["username"].ToString(), _videoClip.CutVideoURL(txtURLClip.Text.Trim()));
                }
                String script1 = "<script language=JavaScript>alert('Cập nhật thành công'); window.location = '../backend/ClipEdit.aspx?productID='"+_productID+";</script>";
                Page.ClientScript.RegisterStartupScript(typeof(Page), "alert", script1);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Redirect(ConstantClass._errorAdminPage);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            txtTitleClip.Text = _dt.Rows[0]["Title"].ToString();
            txtDescription.Text = _dt.Rows[0]["Introduce"].ToString();
        }
    }
}